﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using LotteryAdmin.AdminService;
using System.Windows.Controls.Primitives;
using System.Threading;
using System.Windows.Threading;
using System.Windows.Browser;
namespace LotteryAdmin
{
    public partial class LoginPage : UserControl
    {
        public LoginPage()
        {
            InitializeComponent();
            this.KeyDown +=new KeyEventHandler(LoginPage_KeyDown);
        }

        void LoginPage_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.PlatformKeyCode == 13)
                this.imgBtnLogin_MouseLeftButtonDown(this.imgBtnLogin, null);
        }
        private void imgBtnLogin_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (!Validate.ValidateUserIDModify(this.txtUserName.Text)
                || !Validate.ValidateInputModify(this.pwdPassword.Password))
            {
                MessageBox.Show("用户名或者密码输入了非法字符!");
                return;
            }
            LotteryAdminClient srv = WcfProxy.GetProxy;
            srv.ValidateLoginAsync(this.txtUserName.Text, this.pwdPassword.Password);
            loading.IsBusy=true;
            srv.ValidateLoginCompleted += (s, er) =>
            {
	             loading.IsBusy=false;
                if (er.Result!=string.Empty)
                {
                    App.Session["UserName"] = this.txtUserName.Text;
                    App.Session["LastLoginTime"] = er.Result;
                    App.Session["UserPwd"] = this.pwdPassword.Password;
                    App.Navigation(new MainPage());
                }
                else
                    MessageBox.Show("用户名或者密码错误!");
            };
        }
    }
}
